<template>
  <div class="main">
    <div :class="{'container':active1, 'right-panel-active':active2}">
      <!-- 注册 -->
      <div class="container_form container_signup">
        <el-form class="form" ref="RegisterFormRef" :model="RegisterForm" :rules="RegisterFormRules" v-on:submit.prevent="onSubmit">
          <h2 class="form_title">注册账号</h2>
          <el-form-item prop="userName">
            <el-input type="text" placeholder="用户名" class="input" v-model="RegisterForm.userName" />
          </el-form-item>
          <el-form-item prop="userNickName">
            <el-input type="text" placeholder="昵称" class="input" v-model="RegisterForm.userNickName" />
          </el-form-item>
          <el-form-item prop="userPassword">
            <el-input type="password" placeholder="密码" class="input" v-model="RegisterForm.userPassword" />
          </el-form-item>
          <el-form-item prop="userPhone">
            <el-input type="text" placeholder="手机号" class="input" v-model="RegisterForm.userPhone" />
          </el-form-item>
          <button class="btn" @click="register">点击注册</button>
        </el-form>
      </div>
      <!-- 登录 -->
      <div class="container_form container_signin">
        <el-form class="form login_form" ref="loginFormRef" :model="loginForm" :rules="loginFormRules" v-on:submit.prevent="onSubmit">
          <h2 class="form_title">欢迎登录</h2>
          <el-form-item prop="userName">
            <el-input type="text" placeholder="User" class="input" v-model="loginForm.userName" />
          </el-form-item>
          <el-form-item prop="userPassword">
            <el-input type="password" placeholder="Password" class="input" v-model="loginForm.userPassword" />
          </el-form-item>

          <button class="btn" @click="login">登录</button>
        </el-form>
      </div>

      <div class="container_overlay">
        <div class="overlay">
          <div class="overlay_panel overlay_left">
            <button class="btn" id="signIn" @click="active2 = false">已有账号，直接登录</button>
          </div>
          <div class="overlay_panel overlay_right">
            <button class="btn" id="signUp" @click="active2 = true">没有账号，点击注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      active1: true,
      active2: false,
      RegisterForm: {
        userName: "",
        userNickName: "",
        userPassword: "",
        autoSelect: true,
        autoFix: true,
        userPhone: "",
      },
      // 这是登录表单的数据绑定对象
      loginForm: {
        userName: "admin",
        userPassword: "admin",
      },
      // 这是表单的验证规则对象
      RegisterFormRules: {
        // 验证用户名是否合法
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        userNickName: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        userPassword: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        userPhone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            min: 6,
            max: 11,
            message: "长度在 6 到 11 个字符",
            trigger: "blur",
          },
        ],
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        userName: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        userPassword: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSubmit(e) {
      e.preventDefault();
    },
    // 注册
    register() {
      this.$refs.RegisterFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await this.$axios
          .post("/api/signup", this.RegisterForm)
          .then(() => {
            this.$message.success("注册成功");
            this.$router.push("/");
          })
          .catch(() => {
            this.$message.error("用户名已被占用");
          });
      });
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        await this.$axios
          .post("api/login", this.loginForm)
          .then((response) => {
            if (response.message == "登陆成功") {
              this.$message.success("登录成功");
              window.sessionStorage.setItem("token", response.token);
              this.$router.push("/");
            } else {
              return this.$message.error("用户名或密码错误！");
            }
          })
          .catch((error) => {
            console.log(error);
            this.$message.error("用户名或密码错误！");
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
:root {
  // 字体
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

.main {
  align-items: center;
  background-color: #506587;
  // background: url(../assets/img/bg.jpg);
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: grid;
  height: 100vh;
  place-items: center;
}
.container {
  background-color: #e9e9e9;
  border-radius: 0.7rem;
  border: 0;
  box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25);
  height: 420px;
  max-width: 758px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.container_form {
  height: 100%;
  position: absolute;
  top: 0;
  transition: all 0.6s ease-in-out;
}
.container_signin {
  left: 0;
  width: 50%;
  z-index: 2;
}
.container.right-panel-active .container_signin {
  transform: translateX(100%);
}
.container_signup {
  left: 0;
  opacity: 0;
  width: 50%;
  z-index: 1;
}
.container.right-panel-active .container_signup {
  animation: show 0.6s;
  opacity: 1;
  transform: translateX(100%);
  z-index: 5;
}
.container_overlay {
  height: 100%;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 100;
}
.container.right-panel-active .container_overlay {
  transform: translateX(-100%);
}
.overlay {
  background-color: #acacac;
  // background: url(../assets/img/bg.jpg);
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  left: -100%;
  position: relative;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 200%;
}
.container.right-panel-active .overlay {
  transform: translateX(50%);
}
.overlay_panel {
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  position: absolute;
  text-align: center;
  top: 0;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 50%;
}
.overlay_left {
  transform: translateX(-20%);
}
.container.right-panel-active .overlay_left {
  transform: translateX(0);
}
.overlay_right {
  right: 0;
  transform: translateX(0);
}
.container.right-panel-active .overlay_right {
  transform: translateX(20%);
}
.btn {
  background-color: #0367a6;
  background-image: linear-gradient(90deg, #0367a6 0%, #008997 74%);
  border-radius: 20px;
  border: 1px solid #0367a6;
  color: #e9e9e9;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: bold;
  letter-spacing: 0.1rem;
  padding: 0.9rem 4rem;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}
.form > .btn {
  margin-top: 1.5rem;
}
.btn:active {
  transform: scale(0.95);
}
.btn:focus {
  outline: none;
}
.form {
  background-color: #e9e9e9;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 3rem;
  height: 100%;
  text-align: center;
}
.form_title {
  font-weight: 300;
  margin: 0;
  margin-bottom: 1.25rem;
}
.link {
  color: #333;
  font-size: 0.9rem;
  margin: 1.5rem 0;
  text-decoration: none;
}
.el-form-item {
  width: 100%;
}
.input {
  margin: 0.2rem 0;
}

@keyframes show {
  0%,
  49.99% {
    opacity: 0;
    z-index: 1;
  }

  50%,
  100% {
    opacity: 1;
    z-index: 5;
  }
}
</style>
